//
//  BPHomeViewController.swift
//  BPWeiBo
//  Created by apple on 16/4/6.
//  Copyright © 2016年 apple. All rights reserved.

import UIKit
import SVProgressHUD
import AFNetworking
import SDWebImage
//设置重用ID
private let ID = "homeCell"
//继承基类BPBaseController
class BPHomeViewController: BPBaseController {
    
    //懒加载ViewModel
    private lazy var statusViewModel:BPStatusListViewModel = BPStatusListViewModel()
    
    //懒加载转圈圈的视图
    private lazy var juhua:UIActivityIndicatorView =  {
        let v = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
        
        //让视图转动
        //v.startAnimating()
        return v
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpUI()
        
        if userLogin {
            creatItem()
            resginCell()
            loadHomeInfoArr()
        }
        
        //view.backgroundColor = UIColor.redColor()
        
    }
    
    //MARK:定义我们自定义的refersh的控制器的属性
    private var bapiRefershControl = BPRefreshControl()
    
    //MARK:注册cell
    private func resginCell() {
        tableView.registerClass(BPHomeTableViewCell.self, forCellReuseIdentifier: ID)
        //tableView.rowHeight = 250
        //设置行高为自动计算
        tableView.rowHeight = UITableViewAutomaticDimension
        //设置预估行高减少tableView的计算量
        tableView.estimatedRowHeight = 300
        //设置分割线
        tableView.separatorStyle = .None
        
        
        //设置tablview的footerView为这个转圈圈的视图
        tableView.tableFooterView = juhua

        
        //设置加载的转圈圈的控制器,如果用自定义的控制器来控制我们的刷新控件,就需要将我们自定义的控制器加载tableview中
        //refreshControl = UIRefreshControl()

        tableView.addSubview(bapiRefershControl)
        
        //给转圈圈的视图添加点击事件
        bapiRefershControl.addTarget(self, action: "loadHomeInfoArr", forControlEvents: .ValueChanged)
        
    }
    
    private func setUpUI() {
        view.backgroundColor = UIColor.init(red: 236 / 255.0, green: 236 / 255.0, blue: 236 / 255.0, alpha: 1)
        
        //creatCADisplayLink()
        //MARK:继承基类,实现访客界面的方法
        visiterView.setBaseInfo("关注一些人,回到这里看看有什么惊喜", imgName: "visitordiscover_feed_image_house", cycleicon: UIImage(named: "visitordiscover_feed_image_smallicon")!)

    }
    
    //MARK:设置登录界面的导航条的Item
    private func creatItem() {
        let rightBarItem = UIBarButtonItem(imgName: "navigationbar_pop", title: String(), target: self, action:"push")

        self.navigationItem.rightBarButtonItem = rightBarItem

        let leftBarItem = UIBarButtonItem(imgName: "navigationbar_friendsearch", title: String(), target: self, action: "push")

        self.navigationItem.leftBarButtonItem = leftBarItem
    }
    //MARK:实现点击跳转界面
     @objc private func push() {
        
        let shaoYishao = BPShaoYiShaoViewController()
        
        navigationController?.pushViewController(shaoYishao, animated: true)
    }
    
    //MARK:加载模型数组的方法
    @objc private func loadHomeInfoArr() {
        //根据菊花是否在转动表示是上拉刷新还是下拉加载
        statusViewModel.loadHomeinfoArr(isPulled: juhua.isAnimating()) {
            (success) in
            if !success {
                SVProgressHUD.showErrorWithStatus("数据解析错误")
                return
            }
            
            //不管成功没有都需要停止转动
            //self.refreshControl?.endRefreshing(),自定义就不需要这个方法
            self.bapiRefershControl.endRefreshing()
            
            self.juhua.stopAnimating()
            
            //如果成功就表示返回的是正确的数组
            self.tableView.reloadData()

            //移除遮盖
            SVProgressHUD.dismiss()
        }
        
    }
}

//MARK:tableView的数据源方法
extension BPHomeViewController {
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusViewModel.homeInfoArr.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(ID, forIndexPath: indexPath) as! BPHomeTableViewCell
        
        //设置数据
        cell.cellViewModel = statusViewModel.homeInfoArr[indexPath.row]
        
        //当加载最后一个cell的时候
        if !juhua.isAnimating() && indexPath.row == statusViewModel.homeInfoArr.count - 1 {
            
            juhua.startAnimating()
            
            loadHomeInfoArr()
            
        }
        return cell
    }
    
}

